package com.craftplay.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.craftplay.common.ServerResponse;
import com.craftplay.model.*;
import com.craftplay.service.MomentService;
import com.craftplay.service.OfferService;
import com.craftplay.service.UserService;
import com.craftplay.util.AliMsgSendUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Controller
@RequestMapping("/play")
public class OfferController {

    @Autowired
    HostHolder hostHolder;

    @Autowired
    OfferService offerService;

    @Autowired
    UserService userService;

    @Autowired
    MomentService momentService;

    @RequestMapping(method = RequestMethod.POST, value = "/offer")
    @ResponseBody
    public ServerResponse<String> offer(@RequestBody Offer offer) {
        if (offer != null) {
            offer.setStatus(0);
            offer.setCreatedTime(new Date());

            if (offerService.selectByActorIdAndUnitId(offer.getActorId(), offer.getUnitId()).size() == 0) {
                if (offerService.addOffer(offer) > 0) {
                    //报价成功通知
                    User user = userService.selectUserById(offer.getActorId());
                    Unit unit = momentService.oneMomentDetail(offer.getUnitId());
                    JSONObject json = new JSONObject().fluentPut("unitTitle", unit.getTitle());
                    try {
                        AliMsgSendUtil.sendSms(user.getTel(), "SMS_151175689", json);
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                    return ServerResponse.createBySuccessMessage("报价成功");
                } else {
                    return ServerResponse.createByErrorMessage("报价失败！");
                }
            } else {
                return ServerResponse.createByErrorMessage("已经报价请勿重复提交！");
            }
        }
        return ServerResponse.createByErrorMessage("报价失败！");
    }


    //根据某活动id列出该活动所有尚未处理的报价列表
    @RequestMapping(method = RequestMethod.GET, value = "/alloffer")
    @ResponseBody
    public ServerResponse<Set<OfferInfo>> listAllOffer(@RequestParam("id") int id) {
        List<Offer> offerList = offerService.listAllUnit(id);
        Set<OfferInfo> set = new HashSet<OfferInfo>();
        for (Offer offer : offerList) {
            User user = userService.selectUserById(offer.getActorId());
            OfferInfo offerInfo = new OfferInfo();
            offerInfo.setOfferId(offer.getId());
            offerInfo.setUserId(user.getUserId());
            offerInfo.setImgUrl(user.getHeadUrl());
            offerInfo.setUserName(user.getNickname());
            offerInfo.setSex(user.getSex());
            offerInfo.setOfferPrice(offer.getOfferPrice());
            offerInfo.setPlaceProvided(offer.getPlaceProvided());
            offerInfo.setPlaceFee(offer.getPlaceFee());
            offerInfo.setTool(offer.getTool());
            offerInfo.setToolFee(offer.getToolFee());
            offerInfo.setTimeLen(offer.getTimeLen());
            offerInfo.setLeaveWord(offer.getLeaveWord());
            offerInfo.setCreatedTime(offer.getCreatedTime());
            offerInfo.setStatus(offer.getStatus());
            set.add(offerInfo);
        }
        if (set != null) {
            return ServerResponse.createBySuccess(set);
        } else {
            return ServerResponse.createByErrorMessage("添加失败！");
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/offerdelete")
    @ResponseBody
    public ServerResponse<String> deleteOffer(int[] id) {
        boolean isSuccess = false;
        for (int item : id) {
            if (offerService.updateOffer(item, -1) > 0) {
                isSuccess = true;
            } else {
                isSuccess = false;
            }
        }

        if (isSuccess == true) {
            return ServerResponse.createBySuccessMessage("删除成功");
        } else {
            return ServerResponse.createByErrorMessage("删除失败！");
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/offerpass")
    @ResponseBody
    public ServerResponse<String> addOffer(@RequestParam("id") int id) {
        if (offerService.updateOffer(id, 1) > 0) {
            Offer offer = offerService.getOfferById(id);
            User user = userService.selectUserById(offer.getActorId());
            Unit unit = momentService.oneMomentDetail(offer.getUnitId());
            if (StringUtils.isNotEmpty(user.getTel())) {
                JSONObject json = new JSONObject().fluentPut("unitTitle", unit.getTitle());
                try {
                    AliMsgSendUtil.sendSms(user.getTel(), "SMS_144853235", json);
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }

            return ServerResponse.createBySuccessMessage("添加成功");
        } else {
            return ServerResponse.createByErrorMessage("添加失败！");
        }
    }
}
